/*
Copyright 2010 Farent-Solutions Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 */
package com.farent.solutions.plugins.aribaweb.options;

import com.farent.solutions.plugins.aribaweb.NBawConstants;
import java.io.File;
import javax.swing.JFileChooser;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbPreferences;

public final class AribaWebOptionsPanel extends javax.swing.JPanel {

    private final AribaWebOptionsOptionsPanelController controller;

    AribaWebOptionsPanel(AribaWebOptionsOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        // TODO listen to changes in form fields and call controller.changed()
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        projectLocationLabel1 = new javax.swing.JLabel();
        aribawebHomeTextField = new javax.swing.JTextField();
        awHomeButton = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(AribaWebOptionsPanel.class, "AribaWebOptionsPanel.jPanel1.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel1, org.openide.util.NbBundle.getMessage(AribaWebOptionsPanel.class, "AribaWebOptionsPanel.projectLocationLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(awHomeButton, org.openide.util.NbBundle.getMessage(AribaWebOptionsPanel.class, "AribaWebOptionsPanel.awHomeButton.text")); // NOI18N
        awHomeButton.setActionCommand(org.openide.util.NbBundle.getMessage(AribaWebOptionsPanel.class, "AribaWebOptionsPanel.awHomeButton.actionCommand")); // NOI18N
        awHomeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                awHomeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(projectLocationLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aribawebHomeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(awHomeButton)
                .addGap(35, 35, 35))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectLocationLabel1)
                    .addComponent(aribawebHomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(awHomeButton))
                .addContainerGap(47, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void awHomeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_awHomeButtonActionPerformed
        String command = evt.getActionCommand();
        if ("SET_AW_HOME".equals(command)) {
            JFileChooser chooser = new JFileChooser();
            FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
            chooser.setDialogTitle("Select AribaWeb Home");
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
                File projectDir = chooser.getSelectedFile();
                aribawebHomeTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
            }
        }
}//GEN-LAST:event_awHomeButtonActionPerformed

    void load() {
        aribawebHomeTextField.setText(NbPreferences.forModule(AribaWebOptionsPanel.class).get(NBawConstants.AW_HOME, ""));
    }

    void store() {
        NbPreferences.forModule(AribaWebOptionsPanel.class).put(NBawConstants.AW_HOME, aribawebHomeTextField.getText());
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField aribawebHomeTextField;
    private javax.swing.JButton awHomeButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel projectLocationLabel1;
    // End of variables declaration//GEN-END:variables
}
